package com.jeeplus.modules.statistics.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.statistics.model.blacklist.BlacklistModel;
import com.jeeplus.modules.statistics.service.BlacklistServic;
import com.jeeplus.modules.statistics.util.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/blacklist")
@EnableScheduling
public class BlacklistController {

    private final BlacklistServic servic;

    //@ApiOperation("添加黑名单")
    @PostMapping("addBlacklist")
    public AjaxJson addBlacklist(
            String blacklistName,
            String blacklistCard,
            String blacklistStartTime,
            String blacklistEndTime,
            String blacklistRemark,
            String blacklistEver
    ) {

        BlacklistModel blacklist = servic.findBlacklistByCard(blacklistCard);
        BlacklistModel blacklistModel =new BlacklistModel();
        if (blacklist != null) {
            blacklistModel.setBlacklistId(blacklist.getBlacklistId());
            blacklistModel.setBlacklistStartTimeLast(blacklist.getBlacklistStartTime());
            blacklistModel.setBlacklistEndTimeLast(blacklist.getBlacklistEndTime());
            blacklistModel.setBlacklistRemarkLast(blacklist.getBlacklistRemark());
            blacklistModel.setBlacklistName(blacklistName);
            blacklistModel.setBlacklistCard(blacklistCard);
            blacklistModel.setBlacklistStartTime(blacklistStartTime);
            blacklistModel.setBlacklistEndTime(blacklistEndTime);
            blacklistModel.setBlacklistCount((Integer.parseInt(blacklist.getBlacklistCount()) + 1) + "");
            blacklistModel.setBlacklistRemark(blacklistRemark);
            blacklistModel.setBlacklistEver(blacklistEver);
            blacklistModel.setBlacklistLogic("1");
           int i=servic.updateBlacklist(blacklistModel);
        } else {
            blacklistModel.setBlacklistId(UUID.randomUUID().toString());
            blacklistModel.setBlacklistName(blacklistName);
            blacklistModel.setBlacklistCard(blacklistCard);
            blacklistModel.setBlacklistStartTime(blacklistStartTime);
            blacklistModel.setBlacklistEndTime(blacklistEndTime);
            blacklistModel.setBlacklistCount("1");
            blacklistModel.setBlacklistLogic("1");
            blacklistModel.setBlacklistRemark(blacklistRemark);
            blacklistModel.setBlacklistEver(blacklistEver);
            int i = servic.addBlacklist(blacklistModel);
        }


            return AjaxJson.success("操作成功");



    }


    //@ApiOperation("逻辑删除")
    @PostMapping("logicDelBlacklist")
    public AjaxJson logicDelBlacklist(String blacklistId) {
        BlacklistModel blacklistByid = servic.findBlacklistByid(blacklistId);
            int i = servic.logicDelBlacklist(blacklistByid);
            return AjaxJson.success("操作成功");
    }

    //@ApiOperation("物理删除")
    @PostMapping("delBlacklist")
    public AjaxJson delBlacklist(String blacklistId) {
        BlacklistModel blacklistByid = servic.findBlacklistByid(blacklistId);
            int i = servic.delBlacklist(blacklistByid);
                return AjaxJson.success("操作成功");


    }

    //@ApiOperation("修改黑名单")
    @PostMapping("updateBlacklist")
    public AjaxJson updateBlacklist(
            String blacklistId,
            String blacklistName,
            String blacklistCard,
            String blacklistEndTime,
            String blacklistRemark,
            String blacklistEver,
            String blacklistStartTimeLast,
            String blacklistEndTimeLast,
            String blacklistRemarkLast,
            String blacklistLogic,
            String blacklistCount,
            String blacklistStartTime
    ) {

        BlacklistModel blacklistModel = new BlacklistModel();
            blacklistModel.setBlacklistStartTime(blacklistStartTime);
            blacklistModel.setBlacklistCount(blacklistCount);
            blacklistModel.setBlacklistLogic(blacklistLogic);
            blacklistModel.setBlacklistStartTimeLast(blacklistStartTimeLast);
            blacklistModel.setBlacklistEndTimeLast(blacklistEndTimeLast);
            blacklistModel.setBlacklistRemarkLast(blacklistRemarkLast);
            blacklistModel.setBlacklistId(blacklistId);
            blacklistModel.setBlacklistName(blacklistName);
            blacklistModel.setBlacklistCard(blacklistCard);
            blacklistModel.setBlacklistEndTime(blacklistEndTime);
            blacklistModel.setBlacklistRemark(blacklistRemark);
            blacklistModel.setBlacklistEver(blacklistEver);

            int i = servic.updateBlacklist(blacklistModel);

                return AjaxJson.success("操作成功");

    }

    //@ApiOperation("分页查询黑名单")
    @GetMapping("findBlacklist")
    public AjaxJson findBlacklist(String blacklistName,
                                  String blacklistCard,
                                  @RequestParam("curPage") int curPage,
                                  @RequestParam("pageSize") int pageSize
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("curPage", curPage);
        map.put("pageSize", pageSize);
        BlacklistModel blacklistModel = new BlacklistModel();
        if(null != blacklistName && !"".equals(blacklistName)){
            blacklistModel.setBlacklistName("%"+blacklistName+"%");
        }
        if(null != blacklistCard && !"".equals(blacklistCard)){
            blacklistModel.setBlacklistCard("%"+blacklistCard+"%");
        }
        PageBean<BlacklistModel> blacklist = servic.findBlacklist(map, blacklistModel);
        return AjaxJson.success().put("blacklistData", blacklist);

    }

    //@ApiOperation("id查询黑名单")
    @GetMapping("findBlacklistByid")
    public AjaxJson findBlacklistByid(String id) {
        BlacklistModel blacklistByid = servic.findBlacklistByid(id);

        return AjaxJson.success().put("blacklistData", blacklistByid);
    }
    @Scheduled(cron = "0 0 1 * * ?")
    //@ApiOperation("系统获取当前时间自动清理黑名单")
    @GetMapping("deblocking")
    public void deblocking() {
        int deblocking = servic.deblocking();

    }
}
